package com.yx1996.management;

import com.yx1996.management.Bean.User;
import com.yx1996.management.Config.SpringRootConfig;
import com.yx1996.management.ExcelUtils.DatabaseOperation;
import com.yx1996.management.ExcelUtils.ReadExcel2003;
import com.yx1996.management.ExcelUtils.WriteExcel2003;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@ContextConfiguration(classes = SpringRootConfig.class)
@RunWith(SpringRunner.class)
public class testMyUtiles {


    @Autowired
    private ReadExcel2003 readExcel;

    @Autowired
    private WriteExcel2003 writeExcel;

    @Autowired
    private DatabaseOperation dbo;

    @Test //测试读取Excel表格
    public void testreadExcel2003() {
        try {
            File file = new File("D:\\Users.xls");
            List<User> users = readExcel.readFile(file.getPath());
            System.out.println("成员信息如下：");
            for (User user : users) {
                System.out.println(user.toString());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Test//测试追加数据到Excel表格
    public void testwriteExcel2003() {
        try {
            File file = new File("D:\\Users.xls");
            //写入需要追加的数据
            List<User> users = new ArrayList<>();
            users.addAll(readExcel.readFile(file.getPath()));
            //调用writeFile方法追加数据
            if (writeExcel.writeFile(file.getPath(), users)) {
                System.out.println("追加成功！");
            } else {
                System.out.println("增加出错！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test//测试Excel数据与数据库同步
    public void testSynchronizationToDB() {
        try {
            File file = new File("D:\\Users.xls");
            ArrayList<User> users = (ArrayList<User>) readExcel.readFile(file.getPath());
            if (dbo.SynchronizationToDB(users)) {
                System.out.println("更新成功!");
            } else {
                System.out.println("更新失败!");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    @Test//测试数据库与Excel数据同步
    public void testSynchronizationToexcel() {
        File file = new File("D:\\Users.xls");
        if (dbo.SynchronizationToExcel(file.getPath())) {
            System.out.println("更新成功!");
        } else {
            System.out.println("更新失败!");
        }
    }
}
